clear
set printcolor gs1, permanently
set more off
program drop _all

use "INTERMEDIATE/product_RMS_def10_firm_sampleE_06_15.dta", clear
gen logrevenue  = log(revenue)
label variable logrevenue "Revenue (log)"
egen fe=group(module quarter)
qui tab cohort, gen(cohort_d)
gen sample_B16=1  if (cohort>=3 & cohort<=23)  & maxAge>16  & flag_longitudinal=="Complete" & age<=16 & age>0
save "INTERMEDIATE/temp.dta", replace


use "INTERMEDIATE/firmXgroup_RMS_def10_sampleE_06_15.dta", clear
xtset firmgroup quarter
gen deltaT_fg=D.T2_ijt
bys group quarter: egen N_gt=sum(N_ijt)
cap drop rev_r_gt
bys group quarter: egen revN_r_gt=sum(revN_r_mean_ijt*N_ijt)
gen shareentryR_fg= (revN_r_mean_ijt*N_ijt)/revN_r_gt

collapse (mean) shareentryR_fg  deltaT N_ijt T2_ijt , by(firm group firmgroup lcensored_ij cohort_ij rcensored_ij exitTime_ij maxAge_ij)
gen entryrate2_fg=N_ijt/T2_ijt
bys group: egen tile_shareentryR_fg_g = xtile(shareentryR_fg), nq(3)
save "INTERMEDIATE/tempfirm.dta", replace


use "INTERMEDIATE/firmXgroup_RMS_def10_sampleE_06_15.dta", clear
bys group quarter: egen N_gt=sum(N_ijt)
bys group quarter: egen T2_gt=sum(T2_ijt)
bys group quarter: egen entryrateR_gt=sum(revN_r_mean_ijt*N_ijt)
cap drop rev_r_gt
bys group quarter: egen rev_r_gt=sum(rev_r_mean_ijt*T2_ijt)
drop rev_ijt_rev_gt *cum*
keep group quarter *_gt 
duplicates drop
xtset group quarter
gen entryrate_g=N_gt/T2_gt
gen entryrateR_g=entryrateR_gt/rev_r_gt
gen deltaT_g=D.T2_gt

collapse (mean) entryrateR_g deltaT N_gt T2_gt , by(group)
gen entryrate2_g=N_gt/T2_gt
egen tile_entryrateR_g = xtile(entryrateR_g), nq(3)
egen tile_deltaT_g = xtile(deltaT_g), nq(3)
egen tile2_entryrateR_g = xtile(entryrateR_g), nq(2)
save "INTERMEDIATE/tempgroup.dta", replace


use "INTERMEDIATE/temp.dta", clear
merge m:1 firm group using "INTERMEDIATE/tempfirm.dta", keep(1 3) nogenerate
merge m:1 group using "INTERMEDIATE/tempgroup.dta", keep(1 3) nogenerate
label variable logrevenue "Estimated Sales (log scale)"

foreach num of numlist 5/23 {
gen cohort_nd`num'=cohort_d`num'-((`num'-1)*cohort_d4-(`num'-2)*cohort_d3)
}

foreach sample of varlist sample_B16 {  
foreach var of varlist logrevenue {  
local v1: variable label `var'

preserve
areg `var' i.age#i.tile_entryrateR_g cohort_nd* if `sample'==1, absorb(quarter)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se
keep coef se
seq quarter, f(1) t(3)             
seq age, f(1) t(16) b(3)
gen n = _n
drop if n>48				      
drop n                                                              
reshape wide coef se, i(age) j(quarter)
sort age
foreach num of numlist 1(1)3 {    
gen coef_`num'_u = coef`num' + 1.96*sqrt(se`num')
gen coef_`num'_l = coef`num' - 1.96*sqrt(se`num')
}


foreach num of numlist 1(1)3 {
gen norm_`num' = coef`num'[1]
gen coef_norm_`num' = coef`num'-norm_`num'
gen coef_norm_`num'_u = coef_norm_`num' + 1.96*sqrt(se`num')
gen coef_norm_`num'_l = coef_norm_`num' - 1.96*sqrt(se`num')
}

#delimit;
twoway 
(line coef_norm_1 age if age<=16,color(midblue) lwidth(thick))  
(line coef_norm_2 age if age<=16,color(red) lwidth(thick) lpattern(longdash))  
(line coef_norm_3 age if age<=16,color(midgreen) lwidth(thick) lpattern(shortdash))  
, ytitle("`v1'" " ") xtitle("Age (quarters)") xlabel(4[4]16)  
title("")  leg(on row(1) order(1 "Low" 2 "Medium" 3 "High") region(col(white)))  
graphregion(color(white)) plotregion(fcolor(white)) note(" ") ;	
#delimit cr 
graph export "OUTPUT/Figure6a.eps", replace
restore


preserve
areg `var' i.age#i.tile_shareentryR_fg_g cohort_nd* if `sample'==1, absorb(quarter)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se
keep coef se
seq quarter, f(1) t(3)             
seq age, f(1) t(16) b(3)
gen n = _n
drop if n>48				      
drop n                                                              
reshape wide coef se, i(age) j(quarter)
sort age
foreach num of numlist 1(1)3 {    
gen coef_`num'_u = coef`num' + 1.96*sqrt(se`num')
gen coef_`num'_l = coef`num' - 1.96*sqrt(se`num')
}


foreach num of numlist 1(1)3 {
gen norm_`num' = coef`num'[1]
gen coef_norm_`num' = coef`num'-norm_`num'
gen coef_norm_`num'_u = coef_norm_`num' + 1.96*sqrt(se`num')
gen coef_norm_`num'_l = coef_norm_`num' - 1.96*sqrt(se`num')
}

#delimit;
twoway 
(line coef_norm_1 age if age<=16,color(midblue) lwidth(thick))  
(line coef_norm_2 age if age<=16,color(red) lwidth(thick) lpattern(longdash))  
(line coef_norm_3 age if age<=16,color(midgreen) lwidth(thick) lpattern(shortdash))  
, ytitle("`v1'" " ") xtitle("Age (quarters)") xlabel(4[4]16) title("")  
leg(on row(1) order(1 "Low" 2 "Medium" 3 "High") region(col(white)))  
graphregion(color(white)) plotregion(fcolor(white)) note(" ");	
#delimit cr 
graph export "OUTPUT/Figure6b.eps", replace
restore		
		
	}
}

 
erase "INTERMEDIATE/temp.dta"
erase "INTERMEDIATE/tempfirm.dta"
erase "INTERMEDIATE/tempgroup.dta"
